package com.gaoqi.mq.topics;

import com.gaoqi.mq.utils.RabbitMQ;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.testng.annotations.Test;

/**
 * 该类用于模拟 rabbitmq topic模式 生产者
 */
@Slf4j
public class Provider {

    @SneakyThrows
    @Test
    public void topicSendMessage() {
        //获取mq连接
        Connection rabbitConnection = RabbitMQ.getRabbitConnection();
        //获取通道
        Channel channel = rabbitConnection.createChannel();

        //设置交换机名和类型 topic 交换机类型固定写法
        channel.exchangeDeclare("topics", "topic");
        //设置routeKey
        String routeKey = "user.find";

        //发布消息
        channel.basicPublish("topics", routeKey, null, ("this is topic message in [" + routeKey + "]").getBytes());
        //关闭连接 和通道
        RabbitMQ.closeChannelAndConnection(channel, rabbitConnection);

    }
}
